from django.http import HttpResponse
from rest_framework.views import APIView

from common.common_api import *

# Create your views here.

"""
        新思路前提:  http://127.0.0.1:8000/execute/autotestapi/
        1.已知项目id=1；
        2.选择的环境已确定;
        3.接口执行表中已知被勾选接口id为1，2 ；interface_action=[1,2]
        4.已知常规校验执行=0； 业务执行=1 （常规校验时，只支持一个接口对应一条疏通测试案例）

"""

"""
**待完成：
1.execute = 1的时候；
2.加日志
3.考虑多线程
4.其他：
*args, **kwargs
QuerySet  类型和字典的区别:
        environment_data = Sys_Environment.objects.filter(id=environment_id).values('address', 'env_name')
        environment_data1 = Sys_Environment.objects.filter(id=environment_id).values('address', 'env_name').first()


"""

execute = 0
environment_id = 1
interface_action = [2] #勾选执行的接口列表
teascase_pass = 0
teascase_fail = 0

class AutotestApi(APIView):
    def get(self, request):
        if execute == 0:
            statistics_interface_result = self.autotestapis_views()
            print(f"此时是疏通测试{execute},即将统计结果")
            return HttpResponse(statistics_interface_result)

        elif execute == 1:
            self.autotestapis_views()
            print(f"此时是业务测试{execute},即将统计结果")

        else:
            print("取消，后续加上日志！！！")


    def autotestapis_views(self, *args, **kwargs):
        # 1.获取测试环境信息
        self.environment_data = data_environment_view(environment_id)
        #{'environment_id': 1, 'environment_address': 'http://127.0.0.1:9000/booklist/', 'environment_name': '测试环境'}

        #2. 已知勾选的需要执行的接口id(后续修改成并发)
        for self.interfaces_id in interface_action:
            global teascase_pass, teascase_fail
            teascase_pass = 0
            teascase_fail = 0

        #3. 获取接口信息
            self.test_interfaces_datas = data_interface_view(self.interfaces_id)


        return self.test_interfaces_datas

